/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shellCommands;

import Sessions.EcomCustomersRemote;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import shellCommands.InterShell.Commands;

/**
 *
 * @author SOGOBA
 */
public class ShellCustomer extends ShellSuperClass implements Runnable {
   EcomCustomersRemote ecomCustomerBean;
    private boolean stop = false;

    public ShellCustomer() {
         super();

        try {
            InitialContext sc = new InitialContext();
           //    StringBuffer res = new StringBuffer();
           ecomCustomerBean= (EcomCustomersRemote) sc.lookup("java:global/StoreEcom/StoreEcom-ejb/EcomCustomers");
        } catch (NamingException ex) {
            Logger.getLogger(ShellCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected final synchronized void executeCommand(String commandLine) throws Exception {
        commandLine = commandLine.trim();
        String commandName;
        String option=null;
        if (commandLine.indexOf(' ') >= 0) {
            commandName = commandLine.substring(0, commandLine.indexOf(' '));
            String chaine = commandLine.substring(commandLine.indexOf(' '), commandLine.length());
            option = chaine.substring(0, chaine.indexOf(' '));
        } else {
            commandName = commandLine;
        }
        InterShell command = getCommand(commandName);
        if (command==null){
            System.out.println("Command is not avalaible");
        }
        else {
        if (command.getName() == Commands.store || command.getName() == Commands.product ||command.getName() == Commands.category ) {
            if ("-add".equals(option) || "-remove".equals(option)){
             System.out.println("Privelege problem to run this command");

            } else {
                command.execute(commandLine,null, ecomCustomerBean);    

            }
        } else if (command.getName() == Commands.cart ||command.getName()==Commands.customer || command.getName()==Commands.connect || command.getName()==Commands.help || command.getName()==Commands.language || command.getName()==Commands.quit || command.getName()==Commands.currency || command.getName()==Commands.output){
             command.execute(commandLine, null, ecomCustomerBean);
        }
    }
    }

    @Override
    public void run() {
        String banner;
        banner = (String) super.getContext().getVar("BANNER");
        if (banner != null) {
            System.out.println(banner);
        }
        br = new Scanner(System.in);
        while (stop == false) {
            try {
                String prompt;
                prompt = (String) super.getContext().getVar("PROMPT");
                if (prompt == null) {
                    prompt = "->";
                }
                System.out.println("Veillez saisir une commande");
                System.out.print(prompt);
                //     System.out.println("kjkjhkjhkjhkjhkhkjhk");
                //      line = line.trim();
                line = br.nextLine();
                executeCommand(line);
                //       }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
